/*
 * Created by JFormDesigner on Sun Aug 08 13:40:30 MSK 2021
 */

package ru.apertum.qsystem.client.forms;

import lombok.extern.log4j.Log4j2;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Font;
import java.awt.Window;
import java.util.Objects;

/**
 * Это формочка заставки.
 *
 * @author Evgeniy Egorov
 */
@Log4j2
public class FSplashScreen extends JFrame {
    public FSplashScreen() {
        initComponents();
        labelLogo.setText(FLogin.LOGO_TEXT);
    }

    private static final class SplashRun implements Runnable {
        private final FSplashScreen screen;

        private SplashRun(FSplashScreen screen) {
            this.screen = screen;
        }

        @Override
        public void run() {
            screen.setVisible(true);
            screen.setAlwaysOnTop(true);
        }
    }


    private static FSplashScreen screen;

    /**
     * Создание и показ сплэш-заставки без блокировки запуска второй копии.
     */
    public static void showSplash() {
        screen = new FSplashScreen();
        SwingUtilities.invokeLater(new SplashRun(screen));
    }

    /**
     * Скрытие сплэш-заставки.
     */
    public static void closeSplash() {
        if (screen != null) {
            screen.setVisible(false);
            screen.dispose();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner non-commercial license
        labelLogo = new JLabel();
        labelLabel = new JLabel();
        labelProgressBar = new JLabel();

        //======== this ========
        setAlwaysOnTop(true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setFocusable(false);
        setFocusableWindowState(false);
        setType(Window.Type.UTILITY);
        setAutoRequestFocus(false);
        setUndecorated(true);
        var contentPane = getContentPane();

        //---- labelLogo ----
        labelLogo.setText("<html>logo here from code.");
        labelLogo.setFont(new Font(Font.MONOSPACED, Font.BOLD, 30));

        //---- labelLabel ----
        labelLabel.setIcon(new ImageIcon(getClass().getResource("/ru/apertum/qsystem/client/forms/resources/label.png")));

        //---- labelProgressBar ----
        labelProgressBar.setText("qsystem");
        labelProgressBar.setIcon(new ImageIcon(getClass().getResource("/ru/apertum/qsystem/client/forms/resources/loading.gif")));
        labelProgressBar.setHorizontalTextPosition(SwingConstants.CENTER);
        labelProgressBar.setHorizontalAlignment(SwingConstants.CENTER);

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(labelProgressBar, GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)
                                                .addContainerGap())
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(labelLabel)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(labelLogo, GroupLayout.DEFAULT_SIZE, 373, Short.MAX_VALUE))))
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                        .addComponent(labelLogo, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                                        .addComponent(labelLabel, GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE))
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(labelProgressBar)
                                .addContainerGap(55, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner non-commercial license
    private JLabel labelLogo;
    private JLabel labelLabel;
    private JLabel labelProgressBar;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
